﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Manager;
using Rio.Framework.WCFService;
using WCFClient = Rio.Framework.WCFService.RioWcfServices;
using Telerik.Windows.Controls;
using System;


namespace Rio.Framework.Pages
{
    /// <summary>
    /// The base class of all controls
    /// </summary>
    public class ControlBase : UserControl, INotifyPropertyChanged, IDisposable
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region Attributs

        private static bool? _isInDesignMode;

        #endregion

        #region Properties

        /// <summary>
        /// Get the current User
        /// </summary>
        public User CurrentUser
        {
            get { return GlobalWebContext.CurrentUser; }
        }

        /// <summary>
        /// Return a reference to the Wcf service
        /// </summary>
        public WCFClient.WCFServiceClient GetWcfClient
        {
            get { return ServiceManager.GetRioWCFServiceClient(); }
        }

        /// <summary>
        /// Return a reference to the DataContaxt
        /// </summary>
        public BusinessEntitiesDomainContext GetDataContext
        {
            get
            {
                return GlobalWebContext.BusinessEntitiesDomainContext;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the control is in design mode (running in Blend
        /// or Visual Studio).
        /// </summary>
        public static bool IsInDesignMode
        {
            get
            {
                if (!_isInDesignMode.HasValue)
                {
                    #if SILVERLIGHT
                        _isInDesignMode = DesignerProperties.IsInDesignTool;
                    #else
                        var prop = DesignerProperties.IsInDesignModeProperty;
                        _isInDesignMode
                            = (bool)DependencyPropertyDescriptor
                            .FromProperty(prop, typeof(FrameworkElement))
                            .Metadata.DefaultValue;
                    #endif
                }

                return _isInDesignMode.Value;
            }
        }

        #endregion

        #region Protected functions

        /// <summary>
        /// Initialize the Wait cursor
        /// </summary>
        protected virtual void CursorWait(FrameworkElement p_oFWElement)
        {
            SplashScreenManager.Wait(this);
            p_oFWElement.Cursor = Cursors.Wait ;
        }

        /// <summary>
        /// Restore the normal cursor
        /// </summary>
        protected virtual void CursorNormal(FrameworkElement p_oFWElement)
        {
            SplashScreenManager.Stop(this);
            p_oFWElement.Cursor = Cursors.Arrow;
        }

        /// <summary>
        /// Initialize the Hand cursor
        /// </summary>
        protected virtual void CursorHand(FrameworkElement p_oFWElement)
        {
            SplashScreenManager.Stop(this);
            p_oFWElement.Cursor = Cursors.Hand;
        }

        /// <summary>
        /// Set the mask property of a 'RadMaskedTextBox' with the current value and the total value
        /// </summary>
        protected void SetMaskProperty(RadMaskedTextBox p_oTxtBox, int p_iValue, int p_iCount)
        {
            // Create mask for page search maskedtextbox
            string L_sMask = String.Empty;
            // Disable MaskedTextBox if only one page
            if (p_iCount == 1)
            {
                p_oTxtBox.IsEnabled = false;
            }
            else
            {
                p_oTxtBox.IsEnabled = true;
            }
            // Match number of page
            char[] l_aDigit = p_iCount.ToString().ToCharArray();
            for (int i = 0; i < p_iCount.ToString().Length; i++)
            {
                L_sMask += "9";
            }
            // Protect each character after the real mask 
            L_sMask += "/";
            for (int i = 0; i < p_iCount.ToString().Length; i++)
            {
                L_sMask += "\\" + l_aDigit[i];
            }

            // Set mask property
            p_oTxtBox.Mask = L_sMask;
            p_oTxtBox.Placeholder = ' ';
            // Init index
            p_oTxtBox.Value = p_iValue;
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Virtual functions

        /// <summary>
        /// Must be override in child class
        /// </summary>
        public virtual void InitLabel()
        {}

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public virtual void Dispose()
        {
        }
        #endregion


    }
}
